<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends Admin_Controller {
    public function __construct(){
        parent::__construct();
    }
	public function index($page_no=0){
		$search["user_name"]=$_GET['user_name'];
		$where=array();
		if($search["user_name"]!=""){
			array_push($where,"user_name like '%".$search["user_name"]."%'");
		}
		if($search["submit"]==""){
			array_push($where,"admin.is_delete =0");
		}
		$page_no = max(intval($page_no),1);
		$this->admin->order('id DESC');
		if(count($where)>0)
			$this->admin->where($where);
		// $data=$this->admin
		// 				 ->join("department","department.dept_id=user.dept_id")
        // 				 ->select_all("user.*,department.dept_name",null,null,true,$page_no,"user");
        $data=$this->admin
						 ->select_all("admin.*",null,null,true,$page_no,"admin");
		$data['moudle_name']="管理员管理";
		//p($data);die();
		$data['action_name']="管理员列表";
		$data['pages']=$this->admin->pages($data["total_num"]);
		$data['search']=$search;
		$this->admin_view('lists',$data);
		
	}
	public function edit($id=0){
		$data['action_name']="管理员信息修改";
		if($id>0){
			$data["admin"]=$this->admin->select_one($id);
			// $this->load->model('department_model','department');
			// $res=$this->department->select_all("*",null,true,null,$page_no,"department");
			// $data["dept_list"]=$res["list"];
		}
		$this->admin_view('edit',$data);
	}
	public function add(){

		// $this->load->model('department_model','department');
		// $res=$this->department->select_all("*",null,true,null,$page_no,"department");
		// $data["dept_list"]=$res["list"];
		
		$data['action_name']="添加管理员";
		$this->admin_view('edit',$data);
	}

	public function update($id=0){
		$admin=$this->input->post('admin');
		if($id>0){
			$this->admin->update($admin, array('id='.$id));
		}else{
			$this->admin->insert($admin);
		}
		redirect('admin/admin/index');
	}
	public function update_password(){
		$password=$this->input->post('password');
		$sql="update admin set user_password='".$password."' where id=".$this->session->userdata('user_id');
		$this->admin->execute($sql);
		echo  json_encode(array("message"=>"success"));
	}

	public function delete($id=0){
		$this->admin->delete_one($id);
		redirect('admin/admin/index');
	}


	public function check(){
		$page_no = max(intval($page_no),1);
		//$this->user->order('user_id DESC');
		$user_name=$this->input->post('user_name');
		p($user_name);die();
		$where=array("user_name like '$user_name%'");
		$data=$this->admin->where($where)
						 ->select_all("admin.*",null,null,true,$page_no,"admin");
		$data['moudle_name']="管理员管理";
		//p($data);die();
		$data['action_name']="员工列表";

		$data['pages']=$this->admin->pages($data["total_num"]);
		$this->admin_view('lists',$data);
	}
}